#define _CRT_SECURE_NO_WARNINGS 1
#include"DList.h"
DL* BuyNode()
{
	DL* ret = (DL*)malloc(sizeof(DL));
	if (!ret)
	{
		perror("malloc:");
		exit(-1);
	}
	else
	{
		return ret;
	}
}
DL* DLinit()
{
	DL* ret = BuyNode();
	if (!ret)
	{
		perror("malloc:");
		exit(-1);
	}
	else
	{
		ret->fro = ret;
		ret->def = ret;
	}
	return ret;
}

void DLprint(DL* head)
{
	DL* cur = head->def;
	while (cur != head)
	{
		printf("%d<->", cur->val);
		cur = cur->def;
	}
	printf("\n");
}
void DLpushback(DL* head,Mt x)
{
	DL* tem = BuyNode();
	tem->val = x;
	DL* tail = head->fro;
	tail->def = tem;
	tem->fro = tail;
	tem->def = head;
	head->fro = tem;
}
void DLpopback(DL* head)
{
	assert(head->def != head);
	DL* tail = head->fro;
	DL* fro = tail->fro;
	free(tail);
	tail = NULL;
	fro->def = head;
	head->fro = fro;
}
void DLpushfront(DL* head,Mt x)
{
	DL* ne = head->def;
	DL* ret = BuyNode();
	ret->val = x;
	ne->fro = ret;
	ret->def = ne;
	head->def = ret;
}
void DLpopfront(DL* head)
{
	assert(head->def != head);
	DL* ne = head->def->def;
	DL* ta = head->def;
	free(ta);
	ta = NULL;
	head->def = ne;
	ne->fro = head;
}
DL* DLfind(DL* head,Mt x)
{
	DL* cur = head->def;
	while (cur != head)
	{
		if (cur->val == x)
		{
			return cur;
		}
		cur = cur->def;
	}
	return NULL;
}
void DLerase(DL* head, DL* ptr)
{
	DL* fro = ptr->fro;
	DL* def = ptr->def;
	free(ptr);
	ptr = NULL;
	fro->def = def;
	def->fro = fro;
}
void DLInsert(DL* head,DL * ptr,int x)
{
	DL* fro = ptr->fro;
	DL* ret = BuyNode();
	if (ret)
	{
		ret->val = x;
		fro->def = ret;
		ptr->fro = ret;
		ret->fro = fro;
		ret->def = ptr;
	}
	else
	{
		perror("malloc:");
		exit(-1);
	}
	
}